import java.util.ArrayList;
import java.util.List;

public class Solution257 {
    List<String> res;

    public List<String> binaryTreePaths(TreeNode root) {
        res = new ArrayList<>();
        dfs(root, new StringBuilder());
        return res;
    }

    void dfs(TreeNode root, StringBuilder s) {
        int t = s.length();
        if (root.left == null && root.right == null) {
            res.add(new String(s.append(root.val)));
            s.delete(t, s.length());
            return;
        }
        s.append(root.val).append("->");
        if (root.left != null) {
            dfs(root.left, s);
        }
        if (root.right != null) {
            dfs(root.right, s);
        }
        s.delete(t, s.length());
    }
}
